<layout name="page" />
<block name="content">
	<input type="hidden" name="ajax" id="ajax" value="1">
	<select name="folder_list" id="folder_list" class="hidden">
		<option value="0">根节点</option>
		{:fill_option($folder_list)}
	</select>
	{:widget('page_header/simple',array('name'=>$folder_name,'search'=>'N'))}
	<div class="operate-panel">
		<i class="mid"></i>
		<a class="btn btn-normal" onclick="add()">新增</a>
		<a class="btn btn-normal" onclick="save()" >保存</a>
		<i class="split"></i>
		<a class="btn btn-del" onclick="del()">删除</a>
	</div>
	<div class="sub x1 box">
		<div class="w28 m100 frame">
			<div class="tree-menu">
				{:widget('html/tree_menu',array($tree))}
			</div>
		</div>
		<div style="width:16px;"></div>
		<div class="x1 frame m100" >
			<form id="form_data" name="form_data" method="post" style="width:100%;">
				<input type="hidden" name="id" id="id" >
				<input type="hidden" name="opmode" id="opmode" value="">
				<input type="hidden" name="admin" id="admin">
				<input type="hidden" name="write" id="write">
				<input type="hidden" name="read" id="read">
				<div class="box">
					<label class="input-label" for="name">名称*：</label>
					<input class="input" type="text" id="name" name="name" check="required" info="请输入名称">
				</div>
				<if condition="$has_pid">
					<div class="box">
						<label class="input-label" for="folder_name">父节点*：</label>
						<input type="hidden" name="pid" id="pid">
						<input name="folder_name" class="input val" id="folder_name" type="text" info="请选择父节点" check="required" />
						<a class="add-on w6" onclick="select_pid()"> 选择 </a>
					</div>
				</if>
				<div class="box">
					<label class="input-label" for="folder_name">管理：</label>
					<div id="admin_list" class="multi-select-box">
						<span class="data-list"></span>
						<input class="letter" type="text" >
						<label></label>
						<div class="search hidden">
							<ul></ul>
						</div>
					</div>
					<a class="add-on" onclick="select_auth();"><i class="fa fa-user"></i></a>
				</div>
				<div class="box">
					<label class="input-label" for="folder_name">编辑：</label>
					<div id="write_list" class="multi-select-box">
						<span class="data-list"></span>
						<input class="letter" type="text" >
						<label></label>
						<div class="search hidden">
							<ul></ul>
						</div>
					</div>
					<a class="add-on" onclick="select_auth();"><i class="fa fa-user"></i></a>
				</div>
				<div class="box">
					<label class="input-label" for="folder_name">访问：</label>
					<div id="read_list" class="multi-select-box">
						<span class="data-list"></span>
						<input class="letter" type="text" >
						<label></label>
						<div class="search hidden">
							<ul></ul>
						</div>
					</div>
					<a class="add-on" onclick="select_auth();"><i class="fa fa-user"></i></a>
				</div>
				<div class="box">
					<label class="input-label" for="sort">排序：</label>
					<input class="input w10" type="text" id="sort" name="sort" >
				</div>
				<div class="box">
					<label class="input-label" for="sort">状态：</label>
					<select class="select w10"  name="is_del" id="is_del">
						<option  value="0">启用</option>
						<option value="1">禁用</option>
					</select>
					<i class="icon"></i>
				</div>
				<div class="box">
					<label class="input-label" for="remark" >备注：</label>
					<textarea class="textarea" name="remark" rows="5" class="" ></textarea>
				</div>
			</form>
		</div>
	</div>
</block>
<block name="js">
	<script type="text/javascript">
		layui.use('global', function() {
			var $ = layui.jquery;
			var fn = {
				add : function() {
					winopen("{:url('system_folder/add',array('controller'=>$controller,'has_pid'=>$has_pid))}", 560, 470);
				},
				save : function() {
					if ($("#opmode").val() == "") {
						layer.msg("请选择数据");
						return false;
					}
					var admin = [];
					var write = [];
					var read = [];
					$("#admin_list span.data-list a").each(function() {
						$this = $(this);
						name = $this.text();
						data = $this.attr('data');
						admin.push({
							name : name,
							data : data
						});
					});
					$("#write_list span.data-list a").each(function() {
						$this = $(this);
						name = $this.text();
						data = $this.attr('data');
						write.push({
							name : name,
							data : data
						});
					});
					$("#read_list span.data-list a").each(function() {
						$this = $(this);
						name = $this.text();
						data = $this.attr('data');
						read.push({
							name : name,
							data : data
						});
					});

					$("#admin").val(JSON.stringify(admin));
					$("#write").val(JSON.stringify(write));
					$("#read").val(JSON.stringify(read));

					send_form('form_data', '{:url("system_folder/save")}', '', function(ret) {
						if (ret.status == 1) {
							parent.layer.msg(ret.info, {
								time : 1200
							}, function() {
								location.reload(true);
							});
						} else {
							layer.msg(ret.info);
							return false;
						};
					});
				},
				del : function() {
					if ($(".tree-menu a.active").length == 0) {
						layer.msg('请选择要删除的数据');
						return false;
					}
					if ($(".tree-menu a.active + input").length > 0) {
						layer.msg('有子节点不能删除');
						return false;
					}
					layer.msg('确定要删除吗?', {
						time : 0,
						btn : ['确定', '取消'],
						yes : function(index) {
							var vars = $("#form_data").serialize();
							send_ajax("{:url('system_folder/del')}", vars, function(ret) {
								if (ret.status) {
									layer.msg(ret.info, {
										time : 1200
									}, function() {
										location.reload(true);
									});
								}
							});
						},
						no : function(index) {
							layer.close(index);
						}
					});
				},
				select_pid : function() {
					if ($(".tree-menu a.active").length > 0) {
						winopen("{:url('system_folder/select_pid',array('controller'=>APP_NAME))}", 560, 470);
					} else {
						layer.msg('请选择数据');
					}
				},
				select_auth : function() {
					if ($(".tree-menu a.active").length > 0) {
						winopen("{:url('popup/auth')}", 560, 470);
					} else {
						layer.msg('请选择数据');
					}
				},
			};
			win_exp(fn);

			set_return_url(null);
			$(".tree-menu a").click(function() {
				$(".tree-menu  a").removeClass("active");
				$this = $(this);
				$this.addClass("active");
				var vars = {};
				vars.id = $this.attr('node');
				send_ajax("{:url('system_folder/read')}", vars, function(ret) {
					show_data(ret);
				});
				return false;
			});

			function show_data(result) {
				for (var s in result.data) {
					set_val(s, result.data[s]);
				}
				$("#opmode").val("edit");
				$("#admin_list span.data-list").html(contact_conv($("#admin").val()));
				$("#write_list span.data-list").html(contact_conv($("#write").val()));
				$("#read_list span.data-list").html(contact_conv($("#read").val()));
				$("#folder_name").val($("#folder_list option[value='" + $("#pid").val() + "']").text());

			};

		});

	</script>
</block>